<template>
	<view class="waterfall" :style="'background-color:'+bgColor">
		<view v-for="(item, index) in columnData" :key="index" class="column" :style="{ width: columnWidth + 'px' }">
			<view v-for="(childItem, childIndex) in item" :key="childIndex" style="width: 100%"
				:id="'item' + childItem.id" >
				<view class="item"
					:style="'background-color:'+cardBgColor+';margin:'+margin+'rpx;border-radius:'+radius+'rpx;'">
					<block v-for="(itemtu, indextu) in childItem.tuji" :key="indextu">
						<image v-if="indextu ==0" :src="itemtu"  @click="tiezi(childItem.cid,childItem.id)" mode="widthFix" lazy-load
							:style="{height:childItem.height,width: '100%'}">
						</image>
					</block>
					
					<view class="title-info" @click="tiezi(childItem.cid,childItem.id)">
						<view v-if="childItem.tuji && childItem.tuji.length > 0" class="d-shang-10">
							
						</view>
						<view v-else class="d-shang-10 d-wenzi-yanse-hui">{{childItem.neirong}}</view>
						<view class="item-title">{{ childItem.title }}</view>
						<view class="item-desc">{{ childItem.desc }}</view>
					</view>
					<view class="d-neikuang-zuoyou-6-shangxia-10 d-buhuanhang">
						<!--头像-->
						<view class="d-gaokuang-40  d-yuanjiao-40 d-box" @click="getgeren(childItem.uid)">
							<image v-if="childItem.touxiang" :src="childItem.touxiang" mode="aspectFill" class="d-img-22 d-yuanjiao-50 d-beijing-hui"></image>
							<image v-else src="/static/assets/images/avatar.png" mode="aspectFill" class="d-img-22 d-yuanjiao-50 d-beijing-hui"></image>
						</view>
						
						<view class="d-kuan d-zuo-10" @click="getgeren(childItem.uid)">
							<view class="d-wenzi-12 d-hanggao-25 d-shang-5 d-wenzi-yanse-hui d-hang-1">{{childItem.mingcheng}} </view>
						</view>
						<view class="d-kuan  d-neirong-you" :id="'zan'+childItem.id">
							<!--image v-if="childItem.zanzhi == 1" src="/static/images/img/dianzan2.png" class="d-img-22" @click="dianzan(childIndex,childItem.id)"></image>
							<image v-else src="/static/images/img/zan3.png" class="d-img-22" @click="dianzan(childIndex,childItem.id)"></image-->
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			//数据源
			dataList: {
				type: Array,
				required: true,
				default: []
			},
			//显示列数
			column: {
				type: Number,
				required: true,
				default: 2
			},
			//卡片margin(rpx)
			margin: {
				type: Number,
				default: 10
			},
			//卡片圆角(rpx)
			radius: {
				type: Number,
				default: 14
			},
			//页面背景颜色
			bgColor: {
				type: String,
				default: '#f5f6f9'
			},
			//卡片背景颜色
			cardBgColor: {
				type: String,
				default: '#FFFFFF'
			},
		},
		data() {
			return {
				columnData: [],
				columnWidth: 0,
				loading: false,
			};
		},
		watch: {
			dataList: {
				immediate: true,
				deep: true,
				handler(newValue, oldValue) {
					this.$nextTick(()=>{
						this.setColumnWidth()
						this.setData()
					})
				},
			},
			column: {
				immediate: false,
				deep: true,
				handler(newValue) {
					this.$nextTick(()=>{
						this.setColumnWidth()
						this.setData()
					})
				},
			},
		},
		methods: {
			//计算每列的高度
			getElemHeight(index) {
				this.$nextTick(() => {
					var arr = [];
					this.dataList.map((item, index) => {
						uni.getImageInfo({
							src: item.picUrl,
							success: (e) => {
								item.height = (e.height * (this.columnWidth / e.width)) + 'px'
								this.createSelectorQuery().select('#item' + item.id)
									.boundingClientRect(res => {
										arr.push({
											...{
												itemHeight: res.height
											},
											...item
										});
										if (arr.length == this.dataList.length) {
											this.columnData = this.distributeToNArrays(arr,
												this.column);
										}
									}).exec();
							}
						})
					})
				})
			},
			distributeToNArrays(arr, n) {
				let sums = new Array(n).fill(0);
				return arr.reduce(
					(arrays, item) => {
						let minSum = Math.min(...sums);
						let minIndex = sums.indexOf(minSum);
						arrays[minIndex].push(item);
						sums[minIndex] += item.itemHeight;
						return arrays;
					},
					new Array(n).fill().map(() => []),
				)
			},
			setColumnWidth() {
				let width = uni.getSystemInfoSync().windowWidth
				this.columnWidth = Math.floor(width / this.column -6)
			},
			setData() {
				const resultArray = this.dataList.reduce(
					(acc, cur, index) => {
						const targetIndex = index % this.column;
						acc[targetIndex].push(cur);
						return acc;
					},
					Array.from(Array(this.column), () => []),
				);
				this.columnData = resultArray
				this.getElemHeight()
			},
			click(item) {
				this.$emit('click', item)
			},
			// 点赞
			dianzan(tieziid) {
				this.$emit('dianzan', tieziid);
			},
			//帖子
			tiezi(cid,id) {
				this.$emit('tiezi', cid,id);
			},
			//个人
			getgeren(uid) {
				this.$emit('getgeren', uid);
			},
			dianzan(index,id){
				
				this.$emit('dianzan', index,id);
			}
		},
	};
</script>

<style scoped>
	.waterfall {
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
		padding-left: 14rpx;
		padding-right: 0rpx;
	}

	.column {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.item {
		overflow: hidden;
	}

	.title-info {
		padding: 0rpx 20rpx 20rpx 20rpx;
	}

	.item-title {
		font-size: 30rpx;
		color: #333333;
		line-height: 46rpx;
		text-align: justify;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
	}

	.item-desc {
		margin-top: 4rpx;
		font-size: 26rpx;
		color: #666666;
		line-height: 34rpx;
		text-align: justify;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
	}
</style>